package com.wwwarehouse.contract.contract;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.time_pick.ChoosePickerDialog;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.PayMentItemBean;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.customview.AutoClickButton;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * 支付项Fragment
 * Created by ShineF on 2017/10/17 0017.
 */

public class ReleasePayItemFragment extends TheParentFragment implements View.OnClickListener {


    private RelativeLayout mRelPlaceOrder;
    private TextView mWeekPay;
    private TextView mMonthPay;
    private TextView mPlace;
    private ImageView mCheckBox;
    private AutoClickButton mNextButton;
    private String[] weeks = {"不使用周付", "每周一", "每周二", "每周三", "每周四", "每周五", "每周六", "每周日"};
    private ArrayList<String> mWeekData;
    private ArrayList<String> mMonthData;
    private String mPbUkid;
    private boolean isCreate;
    private String mContractUkid;

    @Override
    protected int getContentId() {
        return R.layout.fragment_release_pay_item;
    }

    @Override
    protected void init(View view) {
        mWeekPay = (TextView) view.findViewById(R.id.tv_not_week_pay);
        mMonthPay = (TextView) view.findViewById(R.id.tv_not_month_pay);
        mRelPlaceOrder = (RelativeLayout) view.findViewById(R.id.rel_place_order);
        mCheckBox = (ImageView) view.findViewById(R.id.iv_checkbox);
        mPlace = (TextView) view.findViewById(R.id.tv_place);
        mNextButton = (AutoClickButton) view.findViewById(R.id.confirm_btn);

        mCheckBox.setSelected(false);
        mPlace.setSelected(false);

        mWeekPay.setOnClickListener(this);
        mMonthPay.setOnClickListener(this);
        mRelPlaceOrder.setOnClickListener(this);
        mNextButton.setOnClickListener(this);
        mWeekData = new ArrayList<>();
        mMonthData = new ArrayList<>();
        Collections.addAll(mWeekData, weeks);
        //sb一样的设定
        mMonthData.add("不使用月付");
        for (int i = 1; i < 29; i++) {
            mMonthData.add("每月" + i + "日");
        }

    }


    @Override
    public void setTitle() {
        if (peekFragment() instanceof ReleasePayItemFragment) {
            mActivity.setTitle(getString(R.string.set_payment_method));
        }
    }


    private PayMentItemBean.DataBean bean;

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        if (what == 1 || what == 2) {
            List<PayMentItemBean.DataBean> list = JSON.parseArray(commonClass.getData().toString(), PayMentItemBean.DataBean.class);
            if (list != null && list.size() > 0) {
                bean = list.get(0);
                if (bean != null) {
                    if (StringUtils.isNoneNullString(bean.getPayMonth())) {
                        int index = Integer.valueOf(bean.getPayMonth());
                        if (index >= 0) {
                            mMonthPay.setText(mMonthData.get(index));
                            if (index > 0) {
                                isMonthChoose = true;
                            } else {
                                isMonthChoose = false;
                            }
                        }
                    }
                    if (StringUtils.isNoneNullString(bean.getPayWeek())) {
                        int index = Integer.valueOf(bean.getPayWeek());
                        if (index >= 0) {
                            mWeekPay.setText(mWeekData.get(index));
                            if (index > 0) {
                                isWeekChoose = true;
                            } else {
                                isWeekChoose = false;
                            }
                        }
                    }
                    if (StringUtils.isNoneNullString(bean.getPayOrderFlag())) {
                        if (bean.getPayOrderFlag().equals("0")) {
                            isPlaceChoose = true;
                            mPlace.setSelected(false);
                            mCheckBox.setSelected(false);
                        } else if (bean.getPayOrderFlag().equals("1")) {
                            isPlaceChoose = false;
                            mPlace.setSelected(true);
                            mCheckBox.setSelected(true);
                        }
                    }
                    mNextButton.setEnabled(true);
                } else {
                    mNextButton.setEnabled(false);

                }
            }
        }

    }

    @Override
    public void onReLoad(int code) {

    }

    private boolean isWeekChoose;
    private boolean isMonthChoose;
    private boolean isPlaceChoose;


    @Override
    public void onClick(View v) {

        int i = v.getId();
        if (i == R.id.tv_not_week_pay) {
            new ChoosePickerDialog.Builder(getContext())
                    .setData(mWeekData)
                    .setTitle(getString(R.string.week_payment))
                    .setOnDataSelectedListener(new ChoosePickerDialog.OnSelectedListener() {
                        @Override
                        public void onSelected(String s, int i) {

                            mWeekPay.setText(s);
                            isWeekChoose = !s.equals("不使用周付");
                            if (isWeekChoose || isMonthChoose || isPlaceChoose || mCheckBox.isSelected()) {
                                mNextButton.setEnabled(true);
                            } else {
                                mNextButton.setEnabled(false);
                            }

                        }

                        @Override
                        public void onCancel() {

                        }
                    })
                    .create()
                    .show();
        } else if (i == R.id.tv_not_month_pay) {

            new ChoosePickerDialog.Builder(getContext())
                    .setData(mMonthData)
                    .setTitle(getString(R.string.month_payment))
                    .setOnDataSelectedListener(new ChoosePickerDialog.OnSelectedListener() {
                        @Override
                        public void onSelected(String s, int i) {
                            mMonthPay.setText(s);
                            isMonthChoose = !s.equals("不使用月付");
                            if (isWeekChoose || isMonthChoose || isPlaceChoose || mCheckBox.isSelected()) {
                                mNextButton.setEnabled(true);
                            } else {
                                mNextButton.setEnabled(false);
                            }


                        }

                        @Override
                        public void onCancel() {

                        }
                    })
                    .create()
                    .show();

        } else if (i == R.id.rel_place_order) {
            mCheckBox.setSelected(!mCheckBox.isSelected());
            mPlace.setSelected(!mPlace.isSelected());

            isPlaceChoose = mCheckBox.isSelected();
            if (isWeekChoose || isMonthChoose || isPlaceChoose) {
                mNextButton.setEnabled(true);
            } else {
                mNextButton.setEnabled(false);
            }
        } else if (i == R.id.confirm_btn) {
            popFragment();
            Bundle bundle = new Bundle();
            int weekIndex = -1;
            for (int j = 0; j < weeks.length; j++) {
                if (weeks[j].equals(mWeekPay.getText().toString())) {
                    weekIndex = j;
                }
            }
            int monthIndex = mMonthData.indexOf(mMonthPay.getText().toString());

            if (bean != null) {
                //修改
                if (weekIndex >= 0) {
                    bean.setPayWeek(String.valueOf(weekIndex));
                }
                if (monthIndex >= 0) {
                    bean.setPayMonth(String.valueOf(monthIndex));
                }
                if (mCheckBox.isSelected()) {
                    bean.setPayOrderFlag("1");
                } else {
                    bean.setPayOrderFlag("0");
                }
                bundle.putSerializable(ContractConstant.KEY_BUNDLE_PAYMENTS, bean);
            } else {
                //新建
                PayMentItemBean.DataBean dataBean = new PayMentItemBean.DataBean();
                if (weekIndex >= 0) {
                    dataBean.setPayWeek(String.valueOf(weekIndex));
                }
                if (monthIndex >= 0) {
                    dataBean.setPayMonth(String.valueOf(monthIndex));
                }
                if (mCheckBox.isSelected()) {
                    dataBean.setPayOrderFlag("1");
                } else {
                    dataBean.setPayOrderFlag("0");
                }
                bundle.putSerializable(ContractConstant.KEY_BUNDLE_PAYMENTS, dataBean);
            }
            bundle.putString(ContractConstant.KEY_PAY_ITEM_PBUKID, mPbUkid);
            bundle.putString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID, mContractUkid);
            if (StringUtils.isNoneNullString(getArguments().getString("type")) && getArguments().getString("type").equals(ContractConstant.RELEASE_SUPPLY_FUNCTION_EDIT)) {
                bundle.putString("isModify", "modify");
            }
            PayItemMessageFragment payItemMessageFragment = new PayItemMessageFragment();
            payItemMessageFragment.setArguments(bundle);
            pushFragment(payItemMessageFragment);
        }


    }

    @Override
    public void requestDatas() {
        super.requestDatas();
        if (getArguments() != null) {
            if (StringUtils.isNoneNullString(getArguments().getString(ContractConstant.KEY_PAY_ITEM_PBUKID))) {
                mPbUkid = getArguments().getString(ContractConstant.KEY_PAY_ITEM_PBUKID);
            }
            if (StringUtils.isNoneNullString(getArguments().getString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID))) {
                mContractUkid = getArguments().getString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID);
            }
            isCreate = getArguments().getBoolean(ContractConstant.KEY_BOOLEAN_PAY_ITEM_CREATE);
            if (StringUtils.isNoneNullString(getArguments().getString("type")) && getArguments().getString("type").equals(ContractConstant.RELEASE_SUPPLY_FUNCTION_EDIT)) {
                Map<String, Object> paramsMap = new HashMap<>();
                paramsMap.put("contractUkid", mContractUkid);
                httpPost(ContractConstant.METHOD_CONTRACT_PAY_MENT_ITEM_MODIFY, paramsMap, 2);
                return;
            }
            if (!isCreate) {
                Map<String, Object> paramsMap = new HashMap<>();
                paramsMap.put("pbUkid", mPbUkid);
                httpPost(ContractConstant.PAY_MENTS_DEFAULT, paramsMap, 1, true, "");
            }
        }

    }
}
